package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserToken;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.utils.Md5Util;
import com.alan.wifishare.view.iview.IVUserModifyPwd;

/**
 * @className: UserModifyPwdPS
 * @describtion: UserModifyPwdPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/5 20:07
 */
public class UserModifyPwdPS {

    private IVUserModifyPwd ivUserModifyPwd;
    private UserToken userToken;
    private String userPwd;
    private CallbackListener<Void> callbackListener;

    public UserModifyPwdPS(IVUserModifyPwd ivUserModifyPwd) {
        this.ivUserModifyPwd = ivUserModifyPwd;
    }

    /**
     * 提交修改登录密码
     */
    public void submitNewLoginPwd(){
        if(!isSubmitValid()){
            return;
        }
        initCallBackListener();
        userPwd = Md5Util.stringToMd5(userPwd);
        ivUserModifyPwd.showProgress();
        GlobalField.businessHttp.postNewPasswordToModifyLoginPassword(userToken.getUserId(), userPwd, userToken.getToken(), callbackListener);
    }

    /**
     * 提价修改支付密码
     */
    public void submitNewPayPwd() {
        if (!isSubmitValid()) {
            return;
        }
        initCallBackListener();
        userPwd = Md5Util.stringToMd5(userPwd);
        ivUserModifyPwd.showProgress();
        GlobalField.businessHttp.postNewPasswordToModifyPayPassword(userToken.getUserId(), userPwd, userToken.getToken(), callbackListener);
    }

    /**
     * 初始化回调接口
     */
    private void initCallBackListener(){
        if(callbackListener == null){
            callbackListener = new CallbackListener<Void>() {
                @Override
                public void onSuccess(Void result) {
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ivUserModifyPwd.hideProgress();
                            ivUserModifyPwd.modifyPwdSucess();
                        }
                    });
                }

                @Override
                public void onFailure(int errorCode, final String msg) {
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ivUserModifyPwd.hideProgress();
                            ivUserModifyPwd.toastMsg(msg);
                        }
                    });
                }
            };
        }

    }

    private boolean isSubmitValid(){
        userToken = ivUserModifyPwd.getUserToken();
        userPwd = ivUserModifyPwd.getNewPwd();
        String userConfirmPwd = ivUserModifyPwd.getConfirmPwd();
        if(TextUtils.isEmpty(userPwd) || TextUtils.isEmpty(userConfirmPwd)){
            ivUserModifyPwd.toastMsg("密码不能为空");
            return false;
        }
        if(!userConfirmPwd.equals(userPwd)){
            ivUserModifyPwd.toastMsg("两次输入的密码不一致");
            return false;
        }
        return true;
    }
}
